using System;
using Microsoft.Xna.Framework;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework.Input.Touch;
using ShootThemUp.Sources.ScreenManagement;

namespace ShootThemUp.Sources.PlayerInputs.WindowsPhone
{
    /// <summary>
    /// This class determines the player actions and movements in function of the windows phone statement.
    /// The movements are determined by the subclasses.
    /// The player attacks are determined by the touch screen.
    /// </summary>
    public abstract class WindowsPhonePlayerInput : PlayerInput
    {

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="WindowsPhonePlayerInput"/> class.
        /// </summary>
        public WindowsPhonePlayerInput()
        {            
        }

        #endregion
        
        #region Inherited Methods
        
        /// <summary>
        /// Determines if the player is shooting, ie if he is pressing the screen.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if the player is shooting; otherwise, <c>false</c>.
        /// </returns>
        public bool IsShooting()
        {
            // Process touch events
            TouchCollection touchCollection = TouchPanel.GetState();
            bool isShooting = false;
            int index = 0;

            while (index < touchCollection.Count && !isShooting)
            {
                if (touchCollection[index].State == TouchLocationState.Pressed || touchCollection[index].State == TouchLocationState.Moved)
                    isShooting = true;

                index++;
            }

            return isShooting;
        }

        /// <summary>
        /// Speeds of the player ship, in function of the specified time elapsed.
        /// </summary>
        /// <returns>The new speed of the player, depending of the player inputs.</returns>
        public abstract Vector2 Speed();

        /// <summary>
        /// Determines if the player is swaping his weapon.
        /// </summary>
        /// <returns>
        ///   <c>true</c> if the player swaping his weapon; otherwise, <c>false</c>.
        /// </returns>
        public bool SwappingWeapon(InputState input)
        {
            bool swapping = false;
            TouchPanel.EnabledGestures = GestureType.DoubleTap;

            foreach (GestureSample gesture in input.Gestures)
            {
                if(gesture.GestureType == GestureType.DoubleTap)
                {
                    swapping = true;
                    break;
                }
            }
            
            return swapping;            
        }

        #endregion
    }
}
